﻿Imports BITC.DatabaseManagement
Imports Microsoft.SqlServer.Management.Smo

Public Class frmRestoreDatabase

#Region "Declaration"
    Private _server As Server
    Private ReadOnly _restore As Restore
    Public Event RestoreCompleted As EventHandler(Of RestoreCompletedEventArgs)
#End Region

#Region "Constructor"

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _restore = New Restore()
    End Sub

#End Region

#Region "Property"

    Public Property Server() As Server
        Get
            Return _server
        End Get
        Set(ByVal value As Server)
            _server = value
        End Set
    End Property

#End Region

#Region "Method"

#End Region

#Region "Event"

    Private Sub btnOpenFile_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles btnOpenFile.Click
        Try
            Dim result As Boolean = ofdBackupFile.ShowDialog()
            If result Then
                txtBackupFile.Text = ofdBackupFile.FileName
                '_restore.Devices.AddDevice(txtBackupFile.Text, DeviceType.File)

                grdDetail.DataSource = SMOHelper.Current.LoadBackupFile(ofdBackupFile.FileName)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles btnOK.Click
        Try
            If Not String.IsNullOrEmpty(cboDatabase.Text) AndAlso grdDetail.Rows.Count > 0 Then
                SMOHelper.Current.RestoreDatabase(cboDatabase.Text, txtDatabasePath.Text, txtBackupFile.Text)
                RaiseEvent RestoreCompleted(Me, New RestoreCompletedEventArgs() With {.RestoredDatabase = cboDatabase.Text})
                DialogResult = DialogResult.OK
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles btnCancel.Click
        Try
            DialogResult = DialogResult.Cancel
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBrowseFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseFolder.Click
        Try
            fbdDatabaseFile.SelectedPath = txtDatabasePath.Text

            Dim result As Boolean = fbdDatabaseFile.ShowDialog()
            If result Then
                txtDatabasePath.Text = fbdDatabaseFile.SelectedPath
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

#End Region

End Class

Public Class RestoreCompletedEventArgs
    Inherits EventArgs

#Region "Declaration"
    Private m_strRestoredDatabase As String = String.Empty
#End Region

#Region "Constructor"

#End Region

#Region "Property"

    Public Property RestoredDatabase() As String
        Get
            Return m_strRestoredDatabase
        End Get
        Set(ByVal value As String)
            m_strRestoredDatabase = value
        End Set
    End Property

#End Region

#Region "Method"

#End Region

#Region "Event"

#End Region

End Class